\documentclass{article}

\usepackage[top=25mm,bottom=25mm,left=25mm,right=25mm]{geometry}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{subfig}
%\usepackage{epstopdf}
\usepackage[framed,autolinebreaks,useliterate]{mcode}
\setlength{\parindent}{0pt}

\begin{document}

\title{ESP - Digital lab 1}
\author{Floris van Nee \& Simon Dirlik}

\maketitle

\section{Questions}
\subsection{} %1
\begin{lstlisting}
	y=x(1:2:end)
\end{lstlisting}

\subsection{} %2
\begin{lstlisting}
	z=zeros(1,2*length(y)-1)
	z(1:2:end)=y
\end{lstlisting}

\subsection{} %3
\begin{lstlisting}
	z(2:2:end)=mean([y(1:end-1); y(2:end)],1)
\end{lstlisting}

\subsection{} %4
\begin{lstlisting}
	rand(N) % for N*N matrix
	rand(M,N) % for M*N matrix
\end{lstlisting}

\subsection{} %5
\begin{lstlisting}
	toeplitz(5:9,[5 4 3 2 1])
\end{lstlisting}
\clearpage
\section{Problems}
\subsection{} %1
\subsubsection{a}
\begin{lstlisting}
	f0=300;
	fs=8000;
	phi=rand*2*pi;
	x=[0:100];
	y=sin((2*pi*(f0/fs)*x)+phi);

	subplot(2,1,1);
	stem(x,y);
	ylabel('Value x(n)');
	xlabel('Sample number n');

	subplot(2,1,2);
	plot(x,y);
	ylabel('Value x(n)');
	xlabel('Sample number n');
\end{lstlisting}
\begin{figure}[h]
	\begin{center}
		\includegraphics[width=\textwidth]{1_1aplot.eps}
	\end{center}
	\caption{The plots resulting from the matlab code above.}
	\label{fig:1_1aplot}
\end{figure}
The difference between the reconstructed sine and the original sine is that the lines between the sampling points are straight lines, not the actual values.
\clearpage
\subsubsection{b}
\begin{figure}[h]
	\begin{center}
		\includegraphics[width=\textwidth]{1_1bplots.eps}
	\end{center}
	\caption{The plots with $f_0=100,225,350,475$.}
	\label{fig:1_1bplots}
\end{figure}
\clearpage
\subsubsection{c}
\begin{figure}[h]
	\begin{center}
		\includegraphics[width=\textwidth]{1_1cplots.eps}
	\end{center}
	\caption{The plots with $f_0=7525,7650,7775,7900$.}
	\label{fig:1_1cplots}
\end{figure}

Because the sampling frequency is smaller than two times the sampling frequency, aliasing occurs.

\subsubsection{d}
\begin{lstlisting}
mu = 600000; % 18000;
phase = rand*2*pi;
f1 = 4000;
fs = 8000;

n = 0:1/8000:50e-3;
y2 = cos(pi*mu*n.^2+2*pi*f1*n+phase);

subplot(2,1,1);
stem(n,y2);
xlabel('Sample number n');
ylabel('Value y(n)');

subplot(2,1,2);
plot(n,y2);
xlabel('Sample number n');
ylabel('Value y(n)');
\end{lstlisting}

The frequency range is between 4000 Hz and 34000 Hz. Because with aliasing, the observed frequency is lowest when it is a multiple of the sample frequency, it is expected that the frequency in the plot seems low for 8000, 16000, 24000 and 32000 Hz. This occurs indeed at these frequencies, as these are obtained at 0.0067s, 0.02s, 0.0333s and 0.0467s.

The other way around, the signal appears at its highest frequency at $4000 + 8000 \cdot n$ with $n$ an integer. This can be seen in the plot as well.

\subsubsection{e}
\begin{lstlisting}
x = 0:1/8000:2;
y = cos(pi*mu*x.^2 + 2*pi*f1*x + phase);
plot(x,y);

wavplay(y,fs);
\end{lstlisting}

To obtain a rate alias five times in two seconds, mu should be chosen to be 18000. This will give a frequency range between 4000 and 40000 Hz.

\clearpage
\subsection{}%2
\begin{lstlisting}
	f0=300;
	fs=8000;
	phi=0.5*2*pi;
	x=0:400;%50ms
	y=sin((2*pi*(f0/fs)*x)+phi);

	bits=4:4:16;
	i=1;
	N=[];
	for b=bits
		y2=bitround(y,b);
		figure(1);
		subplot(2,2,i);
		hist(y2-y);
		title(sprintf('hist with %d bits',b));
		xlabel('aantal');
		ylabel('foutmarge');

		figure(2);
		subplot(2,2,i);
		stem(x,y2-y);
		title(sprintf('plot with %d bits',b));
		xlabel('n');
		ylabel('y difference');
		i=i+1;
	end
\end{lstlisting}

It can be seen in the figures that a larger number of bits will give a more accurate signal. The histogram of the sin function is not uniformly distributed. This is, because it is a periodic signal and the measurements points are always at the same time each period. Thus, the error is also the same each period.

For a uniformly and normally distributed signal the histogram is also uniformly distributed. For the 400 samples that were taken in the figure, it is not very clear, but for a larger number of samples the distribution is clearly uniform.

\begin{figure}[h]
	\begin{center}
		\includegraphics[width=\textwidth]{hist_1_2_sine.eps}
	\end{center}
	\caption{The histograms of the sine with 4, 8, 12 and 16 bits.}
	\label{fig:hist_1_2_sine}
\end{figure}
\begin{figure}[h]
	\begin{center}
		\includegraphics[width=\textwidth]{stem_1_2_sine.eps}
	\end{center}
	\caption{The plots of the sine with 4, 8, 12 and 16 bits.}
	\label{fig:stem_1_2_sine}
\end{figure}
\clearpage
\begin{lstlisting}
	y=randn(1,400);
\end{lstlisting}
\begin{figure}[h]
	\begin{center}
		\includegraphics[width=\textwidth]{hist_1_2_normaldist.eps}
	\end{center}
	\caption{The histograms of the normal distribution with 4, 8, 12 and 16 bits.}
	\label{fig:hist_1_2_normaldist}
\end{figure}
\begin{figure}[h]
	\begin{center}
		\includegraphics[width=\textwidth]{stem_1_2_normaldist.eps}
	\end{center}
	\caption{The plots of the normal distribution with 4, 8, 12 and 16 bits.}
	\label{fig:stem_1_2_normaldist}
\end{figure}
\clearpage
\begin{lstlisting}
	y = -1 + (2.*rand(1,400));
\end{lstlisting}
\begin{figure}[h]
	\begin{center}
		\includegraphics[width=\textwidth]{hist_1_2_uniformdist.eps}
	\end{center}
	\caption{The histograms of the uniform distribution with 4, 8, 12 and 16 bits.}
	\label{fig:hist_1_2_uniformdist}
\end{figure}
\begin{figure}[h]
	\begin{center}
		\includegraphics[width=\textwidth]{stem_1_2_uniformdist.eps}
	\end{center}
	\caption{The plots of the uniform distribution with 4, 8, 12 and 16 bits.}
	\label{fig:stem_1_2_uniformdist}
\end{figure}

\end{document}
